Skip to content

Shared JsonRepository base for all JSON repositories - #69

Merged
CookieLynx merged 3 commits into
mainfrom
feature/48-json-repository-base
Nov 17, 2026
Merged

CookieLynx merged 3 commits into
mainfrom
feature/48-json-repository-base

Conversation

@sonoftirit

@sonoftirit sonoftirit commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

My architectural improvement for M2. Moves the JSON load/save code out of RestaurantRepository into a shared JsonRepository base so customers, carts and orders don't copy it.

  • JsonRepository takes a file path and a model and gives _load_all() and _save_all()
  • RestaurantRepository now just extends it, its public methods didn't change
  • added lock() so two requests saving the same file at the same time don't overwrite each other
  • temp file gets deleted if a save fails for any reason
  • to use it: class CartRepository(JsonRepository[Cart]) and call super().init(file_path, Cart)
  • added tests for the base class, and updated 2 restaurant repo tests that patched the old file path
  • all tests pass

RestaurantRepository now just passes its file path and model to the base,
so customers/carts/orders can reuse the same loading and atomic saving
instead of copying it. Public methods are unchanged.

Updated two repository tests that patched the old module path and added
tests for the base class.
Requests run in parallel threads, so two requests could both load the file
and the second save would wipe out the first one's change. lock() gives one
lock per JSON file that services can hold around load -> change -> save.
Without it the new test only keeps 1 of 8 items.

@CookieLynx CookieLynx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks Great! just one thing I would change.

Currently the _save_all catches only OSError's (disk full, no permission, etc). However if json.dump fails for a reason such as a data type thats invalid (TypeError or ValueError) it will not trigger the OSError block leaving the temp file on the disk forever and erroring the program.

Other then that I approve!

Before, only OSError cleaned it up, so a TypeError/ValueError from json.dump
left a .tmp file behind (from Nathan's review). Cleanup is now in a finally
block. OSError is still wrapped with the file path like before.
@sonoftirit

sonoftirit commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator Author

good catch, fixed it

  • moved the temp file cleanup into a finally block so it gets deleted no matter what error happens
  • added a test for TypeError/ValueError during json.dump

lmk if it looks good now

@CookieLynx CookieLynx left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New changes looking good! All tests pass! Handing of removing the file after regardless of errors is a good fix! Approved!

@CookieLynx
CookieLynx merged commit 015a8bb into main Nov 17, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants